#include <gtk/gtk.h>

/*
菜单栏(GtkMenuBar)是放置菜单构件的容器，菜单栏的尺寸是自适应的
	GtkWidget *gtk_menu_bar_new(void);

菜单构件(GtkMenu)是放置菜单项的容器，也是GtkMenuShell的子类
	GtkWidget *gtk_menu_bar_new(void);
	
GtkMenuBar和GtkMenu的父类是GtkMenuShell,它有3个成员函数：
	gtk_menu_shell_append()
	gtk_menu_shell_prepend()
	gtk_menu_shell_insert()

弹出菜单：
	菜单构件就不用放在菜单栏里，使用函数弹出即可：
	void gtk_menu_popup(GtkMenu *menu,
						GtkWidget *parent_menu_shell,
						GtkWidget *parent_menu_item,
						GtkMenuPositionFunc func,
						gpointer data,
						guint button,
						guint32 activate_time);

*/
GtkWidget * create_menu(void)
{
	GtkWidget *menu;
	GtkWidget *copy;
	GtkWidget *cut;
	GtkWidget *delete;

	GtkAccelGroup *accel_group = gtk_accel_group_new();	//创建快捷方式集合
	
	menu = gtk_menu_new();
	copy = gtk_menu_item_new_with_mnemonic("复制");
	gtk_widget_show(copy);
	//将菜单项加入菜单构件
	gtk_container_add(GTK_CONTAINER(menu), copy);
	// 添加快捷方式
	gtk_widget_add_accelerator(copy, "activate", accel_group, GDK_c, \
								(GdkModifierType)GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);

	cut = gtk_menu_item_new_with_mnemonic("剪切");
	gtk_widget_show(cut);
	gtk_container_add(GTK_CONTAINER(menu), cut);
	gtk_widget_add_accelerator(cut, "activate", accel_group, GDK_x, \
								(GdkModifierType)GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);

	cut = gtk_menu_item_new_with_mnemonic("删除");
	gtk_widget_show(delete);
	gtk_container_add(GTK_CONTAINER(menu), delete);
	gtk_widget_add_accelerator(delete, "activate", accel_group, GDK_F4, \
								(GdkModifierType)GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);

	g_signal_connect( (gpointer)copy, "avtivate", G_CALLBACK(on_copy_activate), NULL);
	g_signal_connect( (gpointer)cut, "avtivate", G_CALLBACK(on_cut_activate), NULL);
	g_signal_connect( (gpointer)delete, "avtivate", G_CALLBACK(on_delete_activate), NULL);
	// 使快捷键生效
	gtk_menu_set_accel_group(GTK_MENU(menu), accel_group);
	return menu;
}

static gint button_press(GtkWidget *widget, GtkEvent *event)
{
	if (event->type == GDK_BUTTON_PRESS) {
		GdkEventButton *bevent = (GdkEventButton *)event;
		gtk_menu_popup(GTK_MENU(widget), NULL, NULL, NULL, NULL, bevent->button, bevent->time);
		return 	TRUE;
	}
	return FALSE;
}
int main( int argc, char *argv[])
{
	gtk_init(&argc, &argv);
	GtkWidget *window;
	window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_window_set_title(GTK_WINDOW(window), "弹出菜单演示");
	gtk_window_show();
}
//////////////////////////////////////////////////////////////////
int main( int argc, char *argv[])
{

  GtkWidget *window;
  GtkWidget *vbox;

  GtkWidget *menubar;
  GtkWidget *filemenu;
  GtkWidget *file;
  GtkWidget *quit;

  gtk_init(&argc, &argv);

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
  gtk_window_set_default_size(GTK_WINDOW(window), 250, 200);
  gtk_window_set_title(GTK_WINDOW(window), "menu");

  vbox = gtk_vbox_new(FALSE, 0);
  gtk_container_add(GTK_CONTAINER(window), vbox);

  menubar = gtk_menu_bar_new();
  filemenu = gtk_menu_new();

  file = gtk_menu_item_new_with_label("File");
  quit = gtk_menu_item_new_with_label("Quit");

  gtk_menu_item_set_submenu(GTK_MENU_ITEM(file), filemenu);
  gtk_menu_shell_append(GTK_MENU_SHELL(filemenu), quit);
  gtk_menu_shell_append(GTK_MENU_SHELL(menubar), file);
  gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 3);

  g_signal_connect_swapped(G_OBJECT(window), "destroy",
        G_CALLBACK(gtk_main_quit), NULL);

  g_signal_connect(G_OBJECT(quit), "activate",
        G_CALLBACK(gtk_main_quit), NULL);

  gtk_widget_show_all(window);

  gtk_main();

  return 0;
}
